//
//  XHUseCouponViewController.m
//  umibus
//
//  Created by fengyn on 16/6/24.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHUseCouponViewController.h"

#import "XHUseCouponCell.h"

#import "Services.h"

@interface XHUseCouponViewController ()
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSArray *m_couponArray;

@end

@implementation XHUseCouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [PublicMethod setExtraCellLineHidden:self.ui_tableView];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHUseCouponCell" bundle:nil] forCellReuseIdentifier:XHUseCouponCellIdentifier];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadCouponData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)loadCouponData{
    [ServiceManager getAvailableCouponsWithProduct:self.productDic[@"sequenceNBR"] price:[NSString stringWithFormat:@"%.2f",self.orderPrice] success:^(NSDictionary *data) {
        if ([data isKindOfClass:[NSArray class]]) {
            self.m_couponArray=(NSArray *)data;
            [self.ui_tableView reloadData];
        }
    } failure:^(NSDictionary *data) {
        
    }];
}

- (IBAction)cancelButtonAction:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger rows=self.m_couponArray.count;
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[self configCellWithIndexPath:indexPath];
    return cell;
}

- (UITableViewCell *)configCellWithIndexPath:(NSIndexPath *)indexPath{
    XHUseCouponCell *cell=[self.ui_tableView dequeueReusableCellWithIdentifier:XHUseCouponCellIdentifier];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    NSDictionary *dic=self.m_couponArray[indexPath.row];
    if ([self.selectedDic[@"sequenceNBR"] isEqualToString:dic[@"sequenceNBR"]]) {
        [cell.ui_button setSelected:YES];
    }else{
        [cell.ui_button setSelected:NO];
    }
    cell.ui_detailLabel.text = [NSString stringWithFormat:@"满%@元可用",dic[@"minOrderAmount"]];
    if([dic[@"couponType"]isEqualToString:@"VOUCHER_TICKET"])
    {
        //代金券
        NSString * money = [NSString stringWithFormat:@"%.2f",[dic[@"couponValue"] doubleValue]];
        NSString * lastOne = [money substringWithRange:NSMakeRange(money.length-1,1)];
        NSString * lastTwo = [money substringWithRange:NSMakeRange(money.length-2,1)];
        if ([lastOne doubleValue]==0&&
            [lastTwo doubleValue]==0)
        {
            cell.ui_priceLabel.text = [NSString stringWithFormat:@"%@",dic[@"couponValue"]];
        }
        else if ([lastOne doubleValue]==0&&
                 [lastTwo doubleValue]!=0)
        {
            cell.ui_priceLabel.text = [NSString stringWithFormat:@"%.1f",[dic[@"couponValue"] doubleValue]];
        }
        else if ([lastOne doubleValue]!=0&&
                 [lastTwo doubleValue]!=0)
        {
            cell.ui_priceLabel.text = [NSString stringWithFormat:@"%.2f",[dic[@"couponValue"] doubleValue]];
        }

//        cell.ui_priceLabel.text = [NSString stringWithFormat:@"%@",dic[@"couponValue"]];
        NSString * startTime = [[NSDate dateWithString:dic[@"startTime"]] stringDateYear];
        NSString * endTime = [[NSDate dateWithString:dic[@"endTime"]] stringDateYear];
        cell.ui_timeLabel.text = [NSString stringWithFormat:@"%@ - %@",startTime,endTime];
        if ([dic[@"operatorCode"]isEqualToString:@"10086"])
        {
            [cell.ui_lineView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"geren_bg_yidong"]]];
            if ([dic[@"suitableType"] isEqualToString:@"CALL"]||
                [dic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
            {
                cell.ui_titleLabel.text = @"代金券-移动话费";
            }
            else if ([dic[@"suitableType"] isEqualToString:@"FLOW"]||
                     [dic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
            {
                cell.ui_titleLabel.text = @"代金券-移动流量屯包";
            }
            cell.ui_titleLabel.textColor = [ColorManager colorWithHexString:@"#59A46C"];
            cell.ui_priceLabel.textColor = [ColorManager colorWithHexString:@"#59A46C"];
            cell.ui_yuanLabel.textColor = [ColorManager colorWithHexString:@"#59A46C"];
            cell.ui_zheLabel.textColor = [ColorManager colorWithHexString:@"#59A46C"];
        }
        else if ([dic[@"operatorCode"]isEqualToString:@"10010"])
        {
            [cell.ui_lineView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"geren_bg_liantong"]]];
            if ([dic[@"suitableType"] isEqualToString:@"CALL"]||
                [dic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
            {
                cell.ui_titleLabel.text = @"代金券-联通话费";
            }
            else if ([dic[@"suitableType"] isEqualToString:@"FLOW"]||
                     [dic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
            {
                cell.ui_titleLabel.text = @"代金券-联通流量屯包";
            }
            cell.ui_titleLabel.textColor = [ColorManager colorWithHexString:@"#E45252"];
            cell.ui_priceLabel.textColor = [ColorManager colorWithHexString:@"#E45252"];
            cell.ui_yuanLabel.textColor = [ColorManager colorWithHexString:@"#E45252"];
            cell.ui_zheLabel.textColor = [ColorManager colorWithHexString:@"#E45252"];
        }
        else if ([dic[@"operatorCode"]isEqualToString:@"10000"])
        {
            [cell.ui_lineView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"geren_bg_dianxin"]]];
            if ([dic[@"suitableType"] isEqualToString:@"CALL"]||
                [dic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
            {
                cell.ui_titleLabel.text = @"代金券-电信话费";
            }
            else if ([dic[@"suitableType"] isEqualToString:@"FLOW"]||
                     [dic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
            {
                cell.ui_titleLabel.text = @"代金券-电信流量屯包";
            }
            cell.ui_titleLabel.textColor = [ColorManager colorWithHexString:@"#00AED2"];
            cell.ui_priceLabel.textColor = [ColorManager colorWithHexString:@"#00AED2"];
            cell.ui_yuanLabel.textColor = [ColorManager colorWithHexString:@"#00AED2"];
            cell.ui_zheLabel.textColor = [ColorManager colorWithHexString:@"#00AED2"];
        }
        cell.ui_yuanLabel.hidden=NO;
        cell.ui_zheLabel.hidden=YES;
    }
    else if ([dic[@"couponType"]isEqualToString:@"DISCOUNT_TICKET"])
    {
        //折扣
        NSString * str = [NSString stringWithFormat:@"%.1f",[dic[@"couponValue"] doubleValue]];
        NSString * lastOne = [str substringFromIndex:str.length-1];
        if ([lastOne isEqualToString:@"0"])
        {
            cell.ui_priceLabel.text = [NSString stringWithFormat:@"%@",dic[@"couponValue"]];
        }
        else
        {
            cell.ui_priceLabel.text = [NSString stringWithFormat:@"%@",str];
        }

//        cell.ui_priceLabel.text = [NSString stringWithFormat:@"%@",dic[@"couponValue"]];
        NSString * startTime = [[NSDate dateWithString:dic[@"startTime"]] stringDateYear];
        NSString * endTime = [[NSDate dateWithString:dic[@"endTime"]] stringDateYear];
        cell.ui_timeLabel.text = [NSString stringWithFormat:@"%@ - %@",startTime,endTime];
        if ([dic[@"operatorCode"]isEqualToString:@"10086"])
        {
            [cell.ui_lineView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"geren_bg_yidong"]]];
            if ([dic[@"suitableType"] isEqualToString:@"CALL"]||
                [dic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
            {
                cell.ui_titleLabel.text = @"抵扣券-移动话费";
            }
            else if ([dic[@"suitableType"] isEqualToString:@"FLOW"]||
                     [dic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
            {
                cell.ui_titleLabel.text = @"抵扣券-移动流量屯包";
            }
            cell.ui_titleLabel.textColor = [ColorManager colorWithHexString:@"#59A46C"];
            cell.ui_priceLabel.textColor = [ColorManager colorWithHexString:@"#59A46C"];
            cell.ui_yuanLabel.textColor = [ColorManager colorWithHexString:@"#59A46C"];
            cell.ui_zheLabel.textColor = [ColorManager colorWithHexString:@"#59A46C"];
        }
        else if ([dic[@"operatorCode"]isEqualToString:@"10010"])
        {
            [cell.ui_lineView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"geren_bg_liantong"]]];
            if ([dic[@"suitableType"] isEqualToString:@"CALL"]||
                [dic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
            {
                cell.ui_titleLabel.text = @"抵扣券-联通话费";
            }
            else if ([dic[@"suitableType"] isEqualToString:@"FLOW"]||
                     [dic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
            {
                cell.ui_titleLabel.text = @"抵扣券-联通流量屯包";
            }
            cell.ui_titleLabel.textColor = [ColorManager colorWithHexString:@"#E45252"];
            cell.ui_priceLabel.textColor = [ColorManager colorWithHexString:@"#E45252"];
            cell.ui_yuanLabel.textColor = [ColorManager colorWithHexString:@"#E45252"];
            cell.ui_zheLabel.textColor = [ColorManager colorWithHexString:@"#E45252"];
        }
        else if ([dic[@"operatorCode"]isEqualToString:@"10000"])
        {
            [cell.ui_lineView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"geren_bg_dianxin"]]];
            if ([dic[@"suitableType"] isEqualToString:@"CALL"]||
                [dic[@"suitableType"] isEqualToString:@"INTELLIGENT"])
            {
                cell.ui_titleLabel.text = @"抵扣券-电信话费";
            }
            else if ([dic[@"suitableType"] isEqualToString:@"FLOW"]||
                     [dic[@"suitableType"] isEqualToString:@"FLOWPACKAGE"])
            {
                cell.ui_titleLabel.text = @"抵扣券-电信流量屯包";
            }
            cell.ui_titleLabel.textColor = [ColorManager colorWithHexString:@"#00AED2"];
            cell.ui_priceLabel.textColor = [ColorManager colorWithHexString:@"#00AED2"];
            cell.ui_yuanLabel.textColor = [ColorManager colorWithHexString:@"#00AED2"];
            cell.ui_zheLabel.textColor = [ColorManager colorWithHexString:@"#00AED2"];
        }
        cell.ui_yuanLabel.hidden=YES;
        cell.ui_zheLabel.hidden=NO;
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=110;
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic=self.m_couponArray[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(didSelectedCoupon:)]) {
        if ([self.selectedDic[@"sequenceNBR"] isEqualToString:dic[@"sequenceNBR"]]) {
            [self.delegate didSelectedCoupon:nil];
        }else{
            [self.delegate didSelectedCoupon:dic];
        }
        
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
